package com.msconfig.mvp.view.fragment;

import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.geo.msconfig.R;
import com.msconfig.MyApplication;
import com.msconfig.bean.ConfigInfoBean;
import com.msconfig.device.CommandSendManage;
import com.msconfig.device.DeviceManage;
import com.msconfig.device.GetSendCommandList;
import com.msconfig.device.TagSendCommand;
import com.msconfig.event.RenameDetermineEvent;
import com.msconfig.io.RtkResponseDataListener;
import com.msconfig.utils.DataCacheUtils;
import com.msconfig.utils.WriteToFileUtils;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import static com.msconfig.mvp.view.fragment.ApplyConfigFragment.FILENAME;


public class RenameConfigFragment extends DialogFragment implements View.OnClickListener {

    private Context context;
    private TextView tvTitle;
    private EditText etInput;
    private String fileName;


    public static RenameConfigFragment newInstance(Bundle bundle) {
        RenameConfigFragment fragment = new RenameConfigFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        this.context=context;
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_fragment_save_config_dialog, container, false);

        view.findViewById(R.id.tv_cancel).setOnClickListener(this);
        view.findViewById(R.id.tv_determine).setOnClickListener(this);
        tvTitle=view.findViewById(R.id.tv_title);
        etInput=view.findViewById(R.id.et_input);

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        tvTitle.setText("重命名");
        Bundle bundle=getArguments();
        if(bundle!=null){
            fileName=bundle.getString(FILENAME);
            if(fileName!=null) {
                etInput.setText(fileName.substring(0,fileName.indexOf(".")));
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_determine:
                String fileName=etInput.getText().toString().trim();
                EventBus.getDefault().postSticky(new RenameDetermineEvent(fileName));
                dismiss();
                break;
        }
    }

}
